var ns, WXPhotoPicker;
app = new Vue({
    el: '#app',
    data: {
        newform: {
            TaskChargeUser: 0,
            TaskChargeUserName: '',
            TaskMoreUser: [],
        },
        ID: 0,
        IsTeamwork:false,//是否是协同人
        chooseType:0,//0-负责人 1-参与人 2-协同人 
    },
    methods: {
        get_data(){
            var that = this;
            var options = {};
            options.ID = that.ID;    
            options.action = "getworktaskdetail";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    var form=data.data;
                    that.IsTeamwork= form.IsTeamwork;
                    that.newform.TaskChargeUser = form.HandleUserID;
                    that.newform.TaskChargeUserName = form.HandleUserName;
                    //参与人
                    that.newform.TaskMoreUser = [];
                    if (form.PartakeUserList&&form.PartakeUserList.length>0) {
                        var newData = [];
                        form.PartakeUserList.forEach((item) => {
                            var newmodel = { UserID: item.UserID, FinalRealName: item.UserName, HeadImg: item.HeadImg };
                            newData.push(newmodel);
                        });
                        that.newform.TaskMoreUser=newData;
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_save: function () {
            var that = this;
            var options = {};
            options.ID = that.ID;   
            options.action = "worktaskchangepartakeuser";  
            options.HandleUserID = that.newform.TaskChargeUser;   
             //参与人
             var TaskMoreUsers = [];
             if (that.newform.TaskMoreUser.length > 0) {
                that.newform.TaskMoreUser.forEach((item) => {
                     TaskMoreUsers.push(item.UserID);
                 });
             }
            options.PartakeUserIDs = JSON.stringify(TaskMoreUsers);   
            console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name: 'updata_task_detail',
                        });
                        api.sendEvent({
                            name: 'reloadworktasklist',
                        });
                        setTimeout(function () {
                            api.closeWin();
                        }, 300)
                        return
                    }
                    else if (data.error) {
                        api.toast({
                            msg: data.error,
                            duration: 3000,
                            location: 'bottom'
                        });
                    }

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
            });
        },
        //负责人
        doChooseHandleUser(){
            var that = this;
            that.chooseType=0;
            // ns.openWin('repairchooseuser', '选择负责人', {
            //     IsMultiple:false,//是否多选
            //     IsChoose:true,//是否只选择
            // });
            ns.openWin('chooseuser_frm', '选择负责人', {
                source: 'chooseuser',
            });
        },
         //参与人
         doChoosePartakeUser(){
            var that = this;
            that.chooseType=1;
            // ns.openWin('repairchooseuser', '选择参与人', {
            //     IsMultiple:true,
            //     IsChoose:true,
            // });
            ns.openWin('chooseuser_frm', '选择参与人', {
                source: 'transfer',
            }); 
        },
         doRemoveUser:function(item,index){
            var that=this;
            that.newform.TaskMoreUser.splice(index, 1);
        },
        //数据处理
        select_person:function(select_list,type){
            if(type==1){
                var newlist=[];
                select_list.forEach((item)=>{
                    var findindex=app.newform.TaskMoreUser.findIndex((val)=>{
                        return val.UserID==item.UserID;
                    });
                    if(findindex<=-1){
                        newlist.push(item);
                    }
                });
                if(newlist.length>0){
                    app.newform.TaskMoreUser=app.newform.TaskMoreUser.concat(newlist);
                }
            }
            if(type==2){
                var newlist=[];
                select_list.forEach((item)=>{
                    var findindex=app.newform.TaskWorkUser.findIndex((val)=>{
                        return val.UserID==item.UserID;
                    });
                    if(findindex<=-1){
                        newlist.push(item);
                    }
                });
                if(newlist.length>0){
                    app.newform.TaskWorkUser=app.newform.TaskWorkUser.concat(newlist);
                }
            }
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    WXPhotoPicker = api.require('WXPhotoPicker');
    app.ID = api.pageParam.ID || 0;

    if(app.ID>0){
        app.get_data();
    }
    api.addEventListener({
        name: 'choose_user_one'
    }, function (ret, err) {
        if(ret.value.data){
            app.newform.TaskChargeUser=ret.value.data[0].UserID;
            app.newform.TaskChargeUserName=ret.value.data[0].FinalRealName;
        }
    });
    api.addEventListener({
        name: 'choose_user_more'
    }, function (ret, err) {
        app.newform.TaskMoreUser=ret.value.data;
    });

    api.addEventListener({
        name: 'purchase_add'
    }, function (ret) {
        if(ret.value.data){
            if (app.chooseType == 0) {
                app.newform.TaskChargeUser=ret.value.data[0].UserID;
                app.newform.TaskChargeUserName=ret.value.data[0].FinalRealName;
            }
            if (app.chooseType == 1) {
                // app.newform.TaskMoreUser=ret.value.data;
                app.select_person(ret.value.data,1);
            }
        }  
    })
};
